//
//  GCW_CollectionViewController.m
//  MooMusic
//
//  Created by 管晨雯 on 15/10/22.
//  Copyright © 2015年 wjy. All rights reserved.
//

#import "GCW_CollectionViewController.h"
#import "FilmAndSongs.h"
@interface GCW_CollectionViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *collecTableView;

/* 接受数据库中的数据 */
@property (nonatomic, strong) NSMutableArray *fileAndSongsArr;

@end

static NSString *const cellIndertifier = @"cellIndertifier";
@implementation GCW_CollectionViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    
    if (self) {
        self.fileAndSongsArr = [NSMutableArray array];
    }
    
    return self;
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = @"我的收藏";
//    self.view.backgroundColor = [UIColor cyanColor];
    
    /*  读取数据库中的数据 */
    self.fileAndSongsArr = [NSMutableArray arrayWithArray:[FilmAndSongs MR_findAll]];
    

    
    [self creatUITableView];
    [self createBackButton];
}

#pragma mark --- 创建收藏界面
- (void)creatUITableView
{
    self.collecTableView  = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    
    self.collecTableView.delegate = self;
    self.collecTableView.dataSource = self;
    self.collecTableView.separatorStyle =  UITableViewCellSeparatorStyleNone;
    
    
    /* 注册cell*/
    [self.collecTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellIndertifier];
    
    
    
    [self.view addSubview:self.collecTableView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.fileAndSongsArr.count > 0) {
        return self.fileAndSongsArr.count;
    }else
    {
        return 0;
    }
    

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndertifier];
    
    
    if (self.fileAndSongsArr.count > 0) {
        
        cell.textLabel.text = [[self.fileAndSongsArr objectAtIndex:indexPath.row]valueForKey:@"name"];
    }
    /* 在点击状态下禁止高亮 */
    cell.selectedBackgroundView = [[UIView alloc]initWithFrame:cell.frame];
    cell.selectedBackgroundView.backgroundColor = [UIColor clearColor];
    cell.backgroundColor = [UIColor clearColor];

    return cell;
}

#pragma mark 1. 打开编辑开关
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    // 先执行父类的编辑方法
    [super setEditing:editing animated:animated];
    
    // TableView编辑开关
    [self.collecTableView setEditing:editing animated:animated];
}

/* 是否进行编辑 */
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return YES;
}

/* 设置编辑的状态是删除还是添加 */
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

//实现编辑逻辑
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        FilmAndSongs *film = [self.fileAndSongsArr objectAtIndex:indexPath.row];
        [film MR_deleteEntity];
        
        [[NSManagedObjectContext MR_defaultContext]MR_saveToPersistentStoreAndWait];
        
        
        [_fileAndSongsArr removeObjectAtIndex:indexPath.row];
        
        [_collecTableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
        
    }
    
}



#pragma mark - 自定义导航栏返回按钮
- (void)createBackButton
{
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    //    leftButton.frame = CGRectMake( 0, 0, 44, 44);
    //    [leftButton contentRectForBounds:CGRectMake(0, 0, 30, 30)];
//        leftButton.contentEdgeInsets = UIEdgeInsetsMake(15, 15, 15, 15);
    leftButton.frame = CGRectMake(0, 0, 40, 40);
    leftButton.backgroundColor = [UIColor clearColor];
    
    
    [leftButton setImage:[UIImage imageNamed:@"/Users/wen/Moo/MooMusic/MooMusic/Picture/fanhui-8@2x.png"] forState:UIControlStateNormal];
    //        [self.navigationController.navigationBar addSubview:leftButton];
    
    [leftButton addTarget:self action:@selector(button1Cliked:) forControlEvents:UIControlEventTouchUpInside];
    //    [leftButton setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
    // 创建UIBarButtonItem
    //UIBarButtonItem *leftBar = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    // 将leftBar赋给navigation的leftBarButton
    //self.navigationItem.leftBarButtonItem = leftBar;
    
    [self.navigationController.navigationBar addSubview:leftButton];
    
    
}

- (void)button1Cliked: (UIButton *)butoon
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
